<?php

namespace App\Admin\Controllers;

use App\Models\Cate;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Hash;

class ZqController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '战区列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Cate);

        $grid->column('id', __('Id'));
        $grid->column('name', __('战区名'));
        $grid->column('address', __('地址'));
        $states = [
        	'on'  => ['value' => 1, 'text' => '显示', 'color' => 'primary'],
        	'off' => ['value' => 0, 'text' => '隐藏', 'color' => 'default'],
        ];
        $grid->column('cate_status', __('状态'))->switch($states);
        $grid->column('created_at', __('创建时间'));
        $grid->model()->where('parent_id', '=', '0');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Cate::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('name', __('Name'));
        $show->field('address', __('Address'));
        $show->field('parent_id', __('Parent id'));
        $show->field('cate_status', __('Cate status'));
        $show->field('score', __('Score'));
        $show->field('isshow', __('Isshow'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('level', __('Level'));
        $show->field('path', __('Path'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Cate);
        $form->text('name', __('战区名'));
        $form->text('address', __('地址'));
        $form->text('username', __('战区账号'))
            ->creationRules('required|min:6|unique:users',[
                'min' => '账号最少要6位',
                'unique'=>'用户已存在'
            ])
            ->updateRules('required|min:6',[
                'min' => '账号最少要6位'
            ]);
        $form->password('password', __('战区密码'))->default(function ($form) {
            return $form->model()->password;
        });
        $states = [
        	'on'  => ['value' => 1, 'text' => '显示', 'color' => 'primary'],
        	'off' => ['value' => 0, 'text' => '隐藏', 'color' => 'default'],
        ];
        $form->switch('cate_status', __('状态'))->states($states)->default(1);
        //保存前回调
        $form->saving(function (Form $form) {
            if ($form->password && (strlen($form->password) <= 30)) {
                $form->password = Hash::make($form->password);
            }
        });
        $form->saved(function (Form $form) {
            $id = $form->model()->id;
            $user = User::find($id);
            if(empty($user)){
                $user = new User();
            }
            $user->usernmae = $form->username;
            if ($form->password && (strlen($form->password) <= 30)) {
                $user->password = $form->password;
            }
            $user->save();
        });
        return $form;
    }
}
